import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [{
        path: '/',
        // name: 'index',
        component: () =>
            import ('./../views/index.vue'),
        children: [
            { path: '', redirect: '/home' },
            {
                path: '/home',
                name: 'home',
                component: () =>
                    import ('./../views/home.vue')
            },
            {
                path: '/order',
                name: 'order',
                component: () =>
                    import ('./../views/order.vue')
            },
            {
                path: '/me',
                name: 'me',
                component: () =>
                    import ('./../views/me.vue')
            },
            {
                path: '/address',
                name: 'address',
                component: () =>
                    import ('./../views/address.vue')
            },
            {
                path: '/city',
                name: 'city',
                component: () =>
                    import ('./../views/city.vue')
            },
            {
                path: '/search',
                name: 'search',
                component: () =>
                    import ('./../views/search.vue')
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () =>
            import ('./../views/Login.vue')
    },
]
const router = new VueRouter({
        mode: 'history',
        routes
    })
    // 路由守卫
router.beforeEach((to, from, next) => {
    const isLogin = localStorage.ele_login ? true : false;
    if (to.path == '/login') {
        next();
    } else {
        // 是否在登录状态下
        isLogin ? next() : next('/login');
    }
})
export default router